package com.codeoss.framework.db.column;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Column;

@Service("datetimeColumnService")
public class DateTimeColumnService implements IColumnService {

	@Override
	public Object getValue(YunContext yunContext, Column column, ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return rs.getTimestamp(column.getColumnName());
	}

	@Override
	public void setValue(YunContext yunContext, Column column, PreparedStatement ps, int index, Object value)
			throws SQLException {
		// TODO Auto-generated method stub
		if (value == null) {
			ps.setObject(index, value);
		} else if (value instanceof java.util.Date) {
			Timestamp timestamp = new Timestamp(((Date) value).getTime());
			ps.setTimestamp(index, timestamp);
		} else {
			ps.setTimestamp(index, (Timestamp) value);
		}
	}

}
